package com.appspot.moviesessions.interfaces.webapp.dto;

import java.util.Collection;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.collections.TransformerUtils;
import org.springframework.stereotype.Component;




@Component("dtoAssembler")
public class AssemblerImpl implements Assembler {

	@SuppressWarnings("unchecked")
	@Override
	public Schedule assemble(
			Collection<com.appspot.moviesessions.model.Session> sessions) {
		Schedule schedule = new Schedule();
		Collection<com.appspot.moviesessions.model.Movie> movies = CollectionUtils.select(
				CollectionUtils.collect(sessions,
						TransformerUtils.invokerTransformer("getMovie")),
				PredicateUtils.uniquePredicate());
		Collection<com.appspot.moviesessions.model.Location> locations = CollectionUtils.select(
				CollectionUtils.collect(sessions,
						TransformerUtils.invokerTransformer("getLocation")),
				PredicateUtils.uniquePredicate());
		for (com.appspot.moviesessions.model.Movie movie : movies) {
			schedule.add(Movie.fromMovie(movie));
		}
		for (com.appspot.moviesessions.model.Location location : locations) {
	        schedule.add(Location.fromLocation(location));
        }
		for (com.appspot.moviesessions.model.Session session : sessions) {
			schedule.add(Session.fromSession(session));
			
		}
		return schedule;
	}

}
